package 我的Java学习_算法基础.day_03;

public class _045_二叉树 {
    public static void main(String[] args) {
        int[] a = {8,5,10,7,4,9,7,2,23};
        preorder(a,0);
        System.out.println("===================================================");
        inorder(a,0);
        System.out.println("===================================================");
        afterOrder(a,0);
    }
    //先序遍历
    public static void preorder(int[] array,int root){
        if(root>= array.length){
            return;
        }
        System.out.println(array[root]);    //输出根节点
        preorder(array,2*root+1);      //输出左子树
        preorder(array,2*root+2);      //输出右子树
    }
    //中序遍历
    public static void inorder(int[] array,int root){
        if(root>= array.length){
            return;
        }
        inorder(array,2*root+1);        //输出左子树
        System.out.println(array[root]);     //输出根节点
        inorder(array,2*root+2);        //输出右子树
    }
    //后序遍历
    public static void afterOrder(int[] array,int root){
        if(root>= array.length){
            return;
        }
        afterOrder(array,2*root+1);     //输出左子数
        afterOrder(array,2*root+2);     //输出右子树
        System.out.println(array[root]);     //输出根节点
    }
}
